using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour
{
    private bool isGamePlaying = false;
    public AudioSource buttonSFX;
    private static bool gameStarted = false;
    public GameObject mainMenuPanel;
    public GameObject pausePanel;
    public GameObject gameOverPanel;
    public TMP_Text finalScoreTextTMP; // Reference to the final score text
    public TMP_Text currentScoreTextTMP; // Reference to the current score text
    public TargetPanel targetPanel;
    public Cannon cannon;
    public int score = 0;
    private int highScore = 0;
    public TMP_Text scoreTextTMP; // Reference to the score text
    public TMP_Text highScoreTextTMP; // Reference to the high score text
    public int lives = 3;
    public GameObject[] hearts;

    // Start is called before the first frame update

    void Start()
    {
        // Load the high score from PlayerPrefs
        highScore = PlayerPrefs.GetInt("HighScore", 0);
        UpdateScore();
        if (gameStarted)
        {
            StartGame();
        }
    }

    public void StartGame()
    {
        buttonSFX.Play();
        gameStarted = true;
        mainMenuPanel.SetActive(false);
        Time.timeScale = 1;
        Invoke("Begin", 0.5f);
    }

    void Begin()
    {
        isGamePlaying = true;
        targetPanel.ShowNewNumber();
        cannon.InitiateCannon();
    }

    // Update is called once per frame
    void Update()
    {
        // Check for spacebar press to shoot
        //if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && lives != 0 && isGamePlaying)
        if (Input.GetKeyDown(KeyCode.Space) && lives != 0 && isGamePlaying)
        {
            cannon.ShootBubble();
            targetPanel.ClearPanel();
            if (lives != 0)
            {
                Invoke("Begin", 0.5f);
            }
            else
            {
                GameOver();
            }

        }
    }

    public void UpdateScore()
    {
        if(score > highScore)
        {
            highScore=score;
        }
        // Update both current score and highscore UI elements
        scoreTextTMP.text = score.ToString();
        highScoreTextTMP.text = "High Score: " + highScore.ToString();
        PlayerPrefs.SetInt("HighScore", highScore);
        PlayerPrefs.Save();
    }

    public void GameOver()
    {
        isGamePlaying = false;
        finalScoreTextTMP.text = "Your Score: " + score.ToString();
        gameOverPanel.SetActive(true);

    }

    public void Restart()
    {
        // Reload the current scene
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void Pause()
    {
        buttonSFX.Play();
        isGamePlaying = false;
        Time.timeScale = 0;
        currentScoreTextTMP.text = "Your Score: " + score.ToString();
        pausePanel.SetActive(true);
    }

    public void Resume()
    {
        buttonSFX.Play();
        Time.timeScale = 1;
        pausePanel.SetActive(false);
        isGamePlaying = true;
    }

    public void MainMenu()
    {
        buttonSFX.Play();
        gameStarted = false;
        // Reload the current scene with main menu opened
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void ExitGame()
    {
        buttonSFX.Play();
        Application.Quit();
    }

}
